<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/4/9
 * Time: 下午4:48
 */

namespace app\merchant\controller;


use app\common\controller\AdminBase;
use app\common\controller\Platurl;
use app\common\model\CardFreecard;
use app\common\model\CardJcList;
use app\common\model\CardJcLog;
use app\common\model\CardJcUserget;

use app\common\model\CardMember;
use app\common\model\Merchant;
use think\Controller;
use think\Request;
use think\Session;

class Cardjc extends AdminBase
{
    //计次卡列表   ,

    public function cardjclist(Request $request)
    {
        if ($request->isAjax()) {
            $arr = request()->post();
            if (!empty($arr['title'])){
                $map['a.title'] = $arr['title'];
            }
            $map['a.card_id'] = $arr['card_id'];
            $map['a.is_del'] = 1;
//            $map['b.type'] = 1;
            if ($this->get_mer_mid() !== ''){
                $map['a.mid'] = array('in', $this->get_mer_mid());
            }else{
                $map['a.mid'] = '';
            }
            $CardJcList = new CardJcList();
            $list = $CardJcList->alias('a')

                ->where($map)
                ->limit(($arr['pageNumber'] - 1) * $arr['pageSize'], $arr['pageSize'])
                ->field('a.id,a.top_mid,a.mid,a.title,a.card_id,a.creat_user,a.price,a.pay_num,a.store,a.num,a.creat_time,a.term_of,a.rec_num')->select();
//            if ($list[0]['id'] == null){
//                $list = [];
//            }
            $lists = [];
            foreach ($list as $k=>$v){
                $lists[$k] = $v;
                $lists[$k]['mach'] = getmachname($v['store']);
                $lists[$k]['creat_time'] = date('Y-m-d h:i:s',$v['creat_time']);
            }


            $re_param["total"] = $CardJcList->where(['card_id'=>$arr['card_id']])->count('id');
            $re_param['sql'] = $map;
            //获取的记录
            $re_param["rows"] = $lists;
            return json($re_param);
        }else{
            $arr = request()->param();
            if (!empty($arr['card_id'])) {
                $card_id = $arr['card_id'];
            }else{
                $card_id = $this->get_cardus_id();
            }
            //END
            $PaltUrl = new Platurl();

            $url = $PaltUrl->getagenturl('card_id', $card_id);
            $arr['card_id'] = $card_id;
            $info['url'] = $url . '/mobile.php?s=/cardjc/cardjclist/card_id/' . $card_id;
            $this->assign('info', $info);
            $this->assign('card', $arr);
            return $this->fetch();
        }

    }

    //计次卡添加
    public function cardjcadd($card_id)
    {
        if ($this->get_mer_mid() !== ''){
            $map['id'] = array('in', $this->get_mer_mid());
        }else{
            $map['id'] = '';
        }
        $machlist = Merchant::where($map)->select();
        $this->assign('machlist', $machlist);
        if (request()->isPost()) {
            $CardJcList = new CardJcList();
            $data = request()->post();
            if ($data['price'] > 0) {
                $data['top_mid'] = Session::get('login_top_mid');
                $data['mid'] = $this->getmid($card_id);
                $data['card_id'] = $card_id;
                $data['creat_user'] = Session::get('user.nickname');
                $data['hx_pass'] = MD5($data['hx_pass']);
                $data['store'] = implode(",", $data['store']);
                $data['creat_time'] = time();
                $add = $CardJcList->allowField(true)->save($data);
                if ($add) {
                    $this->success('添加成功', url('cardjc/cardjclist', ['card_id' => $card_id]));
                } else {
                    $this->error('添加失败', url('cardjc/cardjclist', ['card_id' => $card_id]));
                }
            }else{
                $this->error('添加失败,价格需大于0元', url('cardjc/cardjclist', ['card_id' => $card_id]));
            }
        } else {
            $CardMember = new CardMember();
            $mapc['card_id'] = $card_id;
            $card = $CardMember->where($mapc)->field('title,brand_name,card_id')->find();
            $this->assign('card',$card);

            return $this->fetch();
        }
    }


    public function cardjcinfo(){

        $arr = request()->param();
        $map['id']=request()->param('id');
        $CardJcList = new CardJcList();
        $map['is_del'] = 1;
        if ($this->get_mer_mid() !== ''){
            $map['mid'] = array('in', $this->get_mer_mid());
        }else{
            $map['mid'] = '';
        }
        $info = $CardJcList->where($map)->find();
        $this->assign('info', $info);
        $this->assign('card',$arr);
        return $this->fetch();
    }

    //计次卡修改
    public function cardjcmodify()
    {
        if ($this->get_mer_mid() !== ''){
            $map['id'] = array('in', $this->get_mer_mid());
        }else{
            $map['id'] = '';
        }
        $machlist = Merchant::where($map)->select();
        $this->assign('machlist', $machlist);
        if (request()->isPost()) {
            $CardJcList = new CardJcList();
            $data = request()->post();
            if ($data['price'] > 0) {
                $data['store'] = implode(",", $data['store']);
                if (!empty($data['hx_pass'])) {
                    $data['hx_pass'] = MD5($data['hx_pass']);
                } else {
                    unset($data['hx_pass']);
                }
//            return json($data);
                $add = $CardJcList->allowField(true)->save($data, array('id' => $data['id']));
                if ($add) {
                    $this->success('修改成功', url('cardjc/cardjclist', ['card_id' => $data['card_id']]), 1);
                } else {
                    $this->error('修改失败', url('cardjc/cardjclist', ['card_id' => $data['card_id']]), 1);
                }
            }else{
                $this->error('修改失败,价格需大于0元', url('cardjc/cardjclist', ['card_id' => $data['card_id']]), 1);
            }
        } else {
            $arr = request()->param();
            $map['id'] = request()->param('id');
            $map['is_del'] = 1;
            if ($this->get_mer_mid() !== ''){
                $map['mid'] = array('in', $this->get_mer_mid());
            }else{
                $map['mid'] = '';
            }
            $CardJcList = new CardJcList();
            $info = $CardJcList->where($map)->find();
            $storelist = explode(",", $info['store']);
            $storelist = $info['store'];
            $this->assign('storelist',$storelist);
            $this->assign('card',$arr);
            $this->assign('info', $info);
            return $this->fetch();
        }
    }
    //计次卡删除
    public function cardjcdel()
    {
        $map['id'] = request()->param('id');
        $CardJcList = new CardJcList();
        $CardJcLog = new CardJcLog();
            $CardJcUserget = new CardJcUserget();
            $maps['tims_id'] = $map['id'];
            $maps['end_time'] = ['gt',time()];
            $maps['surplus_num'] = ['gt',0];
        if ($this->get_mer_mid() !== ''){
            $maps['mid'] = array('in', $this->get_mer_mid());
        }else{
            $maps['mid'] = '';
        }
            $user = $CardJcUserget->where($maps)->find();
            if (!$user){
                $data['is_del']=2;
                $del = $CardJcList->save($data,$map);
                $dels = $CardJcLog->save($data,['cardjc_id'=>$map['id']]);
                $delss = $CardJcUserget->save($data,['tims_id'=>$map['id']]);
                if ($del ) {
                    $this->success('删除成功','cardjc/cardjclist','', 1);
                } else {
                    $this->error('删除失败','cardjc/cardjclist','', 1);
                }
            }else{
                $this->error('还有未核销完的用户，不可删除','cardjc/cardjclist','', 1);
            }

    }

    //计次卡领取列表

    public function cardjcuserlist(Request $request)
    {

        if ($request->isAjax()) {
            $arr = $request->post();
            $map['a.tims_id'] = $request->post('tims_id');
            $map['a.is_del'] = 1;
            if (!empty($arr['code'])){
                $map['a.code'] = $arr['code'];
            }
            if ($this->get_mer_mid() !== ''){
                $map['a.mid'] = array('in', $this->get_mer_mid());
            }else{
                $map['a.mid'] = '';
            }
            $CardJcUserget = new CardJcUserget();
            $list = $CardJcUserget->alias('a')
                ->join('wn_card_member b', 'a.card_id=b.card_id', 'left')
                ->join('wn_card_jc_list c', 'a.tims_id=c.id', 'left')
                ->join('wn_card_user_get d', 'a.code=d.code', 'left')
                ->limit(($arr['pageNumber'] - 1) * $arr['pageSize'], $arr['pageSize'])
                ->where($map)->field('a.id,a.mid,a.card_id,c.title,a.num,a.surplus_num,a.code,a.addtime,a.end_time,a.term_of,b.logo_url,b.brand_name,d.nickname')->select();
            $re_param["sql"] = $CardJcUserget->getLastSql();
            $re_param["total"] = $CardJcUserget->alias('a')->where($map)->count('a.id');
            //获取的记录

            $re_param["rows"] = collection($list)->toArray();
            return json($re_param);
        }else{
            $arr = $request->param();
            $this->assign('info',$arr);
            return $this->fetch();
        }

    }

    //计次卡核销充值明细
    public function cardjclog(Request $request)
    {
        if ($request->isAjax()) {
            $CardJcLog = new CardJcLog();
            $param = $request->post();

            if (!empty($param['search_date'])) {
                $date = explode(" - ", $param['search_date']);
                $start_time = strtotime($date[0]);
                $end_time = strtotime($date[1]);
                $map['a.addtime'] = array(array('EGT', $start_time), array('ElT', $end_time));
            }
            if ($this->get_mer_mid() !== ''){
                $map['a.mid'] = array('in', $this->get_mer_mid());
            }else{
                $map['a.mid'] = '';
            }
            $map['a.is_del'] = 1;
            $map['a.user_id'] = $param['user_id'];
            $map['a.mid'] = ['in', $this->get_mer_mid()];
            $list = $CardJcLog->alias('a')
                ->join('wn_card_user_get b', 'a.us_id=b.id', 'left')
                ->join('wn_merchant c', 'a.mid=c.id', 'left')
                ->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize'])
                ->where($map)->field('a.id,a.mid,a.type,a.num,a.pay_fs,a.user,b.nickname,a.addtime,a.orderid,b.code,a.openid,c.merchant')->select();
            $lists = [];
            foreach ($list as $k=>$v){
                $lists[$k] = $v;
                $lists[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                $lists[$k]['type'] = $v['type'] == 1?'购买':'核销';
                $lists[$k]['pay_fs'] = getpayfs($v['pay_fs']);
            }
            $re_param["total"] = $CardJcLog->alias('a')->where($map)->count('a.id');
            //获取的记录
            $re_param["rows"] = $lists;
            return json($re_param);
        }else{
            $arr = $request->param();
            $this->assign('info',$arr);
            return $this->fetch();
        }

    }

    public function getmid($card_id){
        $CardMember = new CardMember();
        $mid = $CardMember->where(['card_id'=>$card_id])->value('mid');
        return $mid;

    }

}